import api from "../../api/index";

let loopCode;
Page({
  data: {
    checked: false,
    code: "",
  },
  onLoad() {
    this.getCode();
  },
  getCode() {
    clearInterval(loopCode);
    wx.login({
      success: (res) => {
        this.setData({ code: res.code });
      },
    });
    loopCode = setInterval(() => {
      wx.login({
        success: (res) => {
          this.setData({ code: res.code });
        },
      });
    }, 300000);
  },
  onChange(e) {
    this.setData({
      checked: e.detail,
    });
  },
  openShowModal() {
    wx.showModal({
      title: "优机小羊用户协议与隐私政策",
      content:
        "为了您能够正常使用我们的服务，您需要阅读并同意《优机小羊用户协议》和《隐私政策》。",
      cancelText: "不同意",
      confirmText: "同意",
      confirmColor: "#73F0EA",
      success: (res) => {
        if (res.confirm) {
          this.setData({
            checked: true,
          });
        }
      },
    });
  },
  async getPhoneNumber(e) {
    let { code, checked } = this.data;
    if (!checked) return;
    if (e.detail.code) {
      api
        .apiAssistLogin({
          port_form: "manage",
          code,
          phone_code: e.detail.code,
        })
        .then((res) => {
          clearInterval(loopCode);
          if (res.code === 1) {
            wx.setStorageSync("token", res.data.token);
            wx.reLaunch({
              url: "/pages/index/index",
            });
          }else{
            this.getCode()
            wx.showToast({
              title: res.msg,
              icon: 'none'
            })
          }
        });
    } else {
      wx.showToast({
        title: "登录失败",
        icon: "none",
      });
    }
  },
  goAgreement(e) {
    wx.navigateTo({
      url: "/pages/agreement/detail/index?name=" + e.currentTarget.dataset.name,
    });
  },
});
